//
//  ViewController.swift
//  FXEvent
//
//  Created by fanxiaoxin_1987@126.com on 11/22/2021.
//  Copyright (c) 2021 fanxiaoxin_1987@126.com. All rights reserved.
//

import UIKit
import FXEvent
import UICreate

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.add(UIButton(.text("测试"), .color(.systemBlue), .event(self, #selector(self.test))), layout: .center, .size(80, 44))
    }
    
    @objc func test() {
        let controller = SubController()
        controller.methodCalled.for(#selector(self.viewDidAppear(_:))).addBlock { args in
            print("sub appeared: \(args)")
        }
        self.present(controller, animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

class SubController: UIViewController {
    
    let timer = FXTimer()
    var times: Int = 0
    let delayer = FXDelayer(timeout: 5)
    @objc dynamic var kvoTest: String? = "ABC"
    
    lazy var kvo = FXPropertyEvent(for: self, keyPath: \.kvoTest)

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        timer.addBlock { [weak self] in
            self?.times += 1
            debugPrint("Times \(self?.times ?? 0)")
        }
        delayer.addBlock {
            debugPrint("I'm delayer")
        }
        delayer.retime()
        
        self.view.set(.bg(.lightGray)).build {
            UIView().build {
                UIButton(.text("开始"), .event(self, #selector(self.start))).layout(.top, .marginX, .height(40))
                UIButton(.text("结束"), .event(self, #selector(self.stop))).layout(.bottomTop(20), .height, .marginX)
                UIButton(.text("改时间"), .event(self, #selector(self.changeTime))).layout(.bottomTop(20), .height, .marginX)
                UIButton(.text("退出"), .event(self, #selector(self.exit))).layout(.bottomTop(20), .height, .marginX)
                UIButton(.text("重新延时器"), .event(self, #selector(self.retime))).layout(.bottomTop(50), .height, .marginX)
                    .layout(for: .parent, .bottom)
            }.layout(.centerY, .marginX(50))
        }
        
        let label = UILabel(.color(.red))
        self.view.add(label, layout: .bottom(20), .centerX)
        self.kvo.addBlock { value in
            debugPrint("kvo \(value)")
        }
        for i in 0..<20 {
            self.view.propertyChanged.tag.addBlock("TagChanged\(i)") { [weak self] v in
                label.text = v.description
                debugPrint("pc\(i) \(v)")
                self?.view.propertyChanged.tag.removeBlock("TagChanged\(i)")
            }
        }
//        }
//        DispatchQueue.global().async {
//            for i in 20..<40 {
//                self.propertyChanged.kvoTest.addBlock("TagChanged\(i)") { v in
//                    debugPrint("pc\(i) \(v)")
//                }
//            }
//        }
//        DispatchQueue.global().async {
//            for i in 40..<60 {
//                self.propertyChanged.kvoTest.addBlock("TagChanged\(i)") { v in
//                    debugPrint("pc\(i) \(v)")
//                }
//            }
//        }
//        DispatchQueue.global().async {
//            for i in 60..<80 {
//                self.propertyChanged.kvoTest.addBlock("TagChanged\(i)") { v in
//                    debugPrint("pc\(i) \(v)")
//                }
//            }
//        }
//        DispatchQueue.global().async {
//            for i in 80..<100 {
//                self.propertyChanged.kvoTest.addBlock("TagChanged\(i)") { v in
//                    debugPrint("pc\(i) \(v)")
//                }
//            }
//        }
        DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in
            self?.kvoTest = "EFG"
            self?.view.tag = 3
            self?.view.propertyChanged.tag.removeBlock("TagChanged3")
        }
        DispatchQueue.main.asyncAfter(deadline: .now() + 4) { [weak self] in
            self?.kvoTest = nil
            self?.view.tag = 5
        }
    }
    @objc func start() {
        self.timer.start()
    }
    
    @objc func stop() {
        self.timer.stop()
    }
    
    @objc func changeTime() {
        self.timer.interval = 3
    }
    @objc func exit() {
        self.dismiss(animated: true, completion: nil)
    }
    @objc func retime() {
        self.delayer.retime()
    }
}
